<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Wishlist extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('bantuan_helper');
		$this->load->helper('text');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		endif;
	}

	function index()
	{
		if(!$this->session->userdata('sort_wishlist')):
			$this->session->set_userdata('sort_wishlist','desc');
		endif;
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/wishlist/index';
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		if($this->session->userdata('sort_wishlist')=='desc'):
			$config['total_rows'] = count($this->get_data('desc'));
			$data['res'] = $this->get_data('desc',FALSE,$config['per_page'],$offset);
		else:
			$config['total_rows'] = count($this->get_data($this->session->userdata('sort_wishlist')));
			$data['res'] = $this->get_data($this->session->userdata('sort_wishlist'),FALSE,$config['per_page'],$offset);
		endif;
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Wishlist';
		$data['template'] = 'wishlist/index';
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}

	function view()
	{
		$wishlist_id = $this->uri->segment(4);
		if(!empty($wishlist_id)):
			$data['content'] = $this->get_data('asc',$wishlist_id);
			$data['title'] = 'Detail Wishlist';
			$data['template'] = 'wishlist/view';
			$this->load->view('backend/index',$data);
		else:
			redirect('backend/wishlist','refresh');
		endif;
	}
	function delete()
	{
		$wishlist_id = $this->uri->segment(4);
		if(!empty($wishlist_id)):
			$this->db->where('wishlist_id',$wishlist_id)->delete('tb_wishlist');
		endif;
		redirect('backend/wishlist','refresh');
	}
	
	function get_data($sort=FALSE, $wishlist_id=FALSE, $limit=FALSE, $offset=FALSE)
	{
		if($limit):
			$this->db->limit($limit,$offset);
		endif;
		if($sort):
			if($sort=='asc'):
				$this->db->order_by('wishlist_id','asc');
			elseif($sort=='desc'):
				$this->db->order_by('wishlist_id','desc');
			endif;
		endif;
		if($wishlist_id):
			$this->db->where('wishlist_id',$wishlist_id);
			return $this->db->get('tb_wishlist')->row();
		else:
			return $this->db->get('tb_wishlist')->result();
		endif;
	}
	
	function ajax_bulk_action($param)
	{
		foreach($_POST["wishlist_id"] as $wishlist_id):
			if($param=='delete'):
				$this->db->where('wishlist_id',$wishlist_id)->delete('tb_wishlist');
			endif;
		endforeach;
	}
	
	function sort_wishlist($sort)
	{
		if($sort):
			$this->session->set_userdata('sort_wishlist',$sort);
			redirect('backend/wishlist');
		else:
			redirect('backend/wishlist','refresh');
		endif;
	}
}

/* End of file product_cat.php */
/* Location: ./system/application/controllers/backend/product_cat.php */
